CSharpTest.Net
TryAdd Method
See Also  Example Send Feedback Download Help File
CSharpTest.Net.Library Assembly > CSharpTest.Net.Collections Namespace > BTreeDictionary<TKey,TValue> Class : TryAdd Method

key
The object to use as the key of the element to add.
value
The object to use as the value of the element to add.

Glossary Item Box

Adds an element with the provided key and value to the IDictionary.

Syntax

Visual Basic (Declaration) 
Public Function TryAdd( _
   ByVal key As TKey, _
   ByVal value As TValue _
) As Boolean
C# 
public bool TryAdd( 
   TKey key,
   TValue value
)

Parameters

key
The object to use as the key of the element to add.
value
The object to use as the value of the element to add.

Exceptions

ExceptionDescription
System.NotSupportedExceptionThe IDictionary is read-only.

Example

Library/Library.Test/TestBTreeDictionary.cs

C#Copy Code
BTreeDictionary<int, string> data = new BTreeDictionary<int, string>(Comparer);
for (int i = 0; i < 100; i++)
    Assert.IsTrue(data.TryAdd(i, i.ToString()));

Assert.AreEqual(50, new List<KeyValuePair<int, string>>(data.EnumerateFrom(50)).Count);
Assert.AreEqual(25, new List<KeyValuePair<int, string>>(data.EnumerateFrom(75)).Count);

for (int i = 0; i < 100; i++)
{
    int first = -1;
    foreach (KeyValuePair<int, string> kv in data.EnumerateFrom(i))
    {
        first = kv.Key;
        break;
    }
    Assert.AreEqual(i, first);
}
VB.NETCopy Code
Dim data As New BTreeDictionary(Of Integer, String)(Comparer)
Dim i As Integer = 0
While i < 100
    Assert.IsTrue(data.TryAdd(i, i.ToString()))
    System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
End While

Assert.AreEqual(50, New List(Of KeyValuePair(Of Integer, String))(data.EnumerateFrom(50)).Count)
Assert.AreEqual(25, New List(Of KeyValuePair(Of Integer, String))(data.EnumerateFrom(75)).Count)

Dim i As Integer = 0
While i < 100
    Dim first As Integer = -1
    For Each kv As KeyValuePair(Of Integer, String) In data.EnumerateFrom(i)
        first = kv.Key
        Exit For
    Next
    Assert.AreEqual(i, first)
    System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
End While

Requirements

Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7

See Also

Generated with Document! X 2011 by Innovasys